<?php
class editAction extends sfActions
{
	public function execute($request)
	{
		$this->customer_id = $request->getParameter('customer_id');
		if ($this->customer_id) {
			$this->customer = Doctrine_Core::getTable('DtCustomer')->find($this->customer_id);
			if ($this->customer)
				$this->customer_form = new DtCustomerForm($this->customer);
		}
		
		if ($request->hasParameter('customer')) {
			$this->customer_form = new DtCustomerForm();
			$form = $request->getParameter('customer');
			$this->customer_form->bind($form);
			if ($this->customer_form->isValid())
				$this->status = $this->updateCustomerFromRequest($this->customer, $form);
			if ($this->status) {
				$this->customer->save();
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updateCustomerFromRequest(&$customer, $form)
	{
		if ($form['phone_number']) {
			$c = Doctrine_Core::getTable('DtCustomer')->findOneByPhoneNumber($form['phone_number']);
			if (!$c || ($c->id == $customer->id)) {
				$customer->phone_number = $form['phone_number'];
			} else {
				return false;
			}
		}
		
		if ($form['name'])
			$customer->name = $form['name'];
		if ($form['email'])
			$customer->email = $form['email'];
		
		$customer->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>